<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Login_c extends CI_Controller {
        public function __construct() {
            parent::__construct();
            $this->load->model('login_m');
            $this->load->helper(array('form', 'url', 'html'));
            $this->load->library('form_validation');
        }
        
        public function index() {
            $data['title'] = 'Login Page';
            $data['username'] = array(
                'name' => 'username',
                'id' => 'username',
                'type' => 'text',
            );
            $data['password'] = array(
                'name' => 'password',
                'id' => 'password',
                'type' => 'password',
            );
            
            $this->load->view('login_v', $data);
        }
        
        function login(){
            $this->form_validation->set_rules('username', 'Username', 'required');
            $this->form_validation->set_rules('password', 'Password', 'required');

            $username = $this->input->post('username');
            $password = $this->input->post('password');

            $result = $this->login_m->validate_user($username, $password);
            $data['session'] = $result;
            $data['title'] = 'Manage Public Content Page';
            if ($result !== FALSE) {
                if ($result === 'admin') {
                    redirect('users');
                } else if ($result === 'viewer'){
                    redirect('authenticated/posts');
                } else{
                    redirect('authenticated/posts');
                }
            } else {
                redirect('home');
            }
        }
    }
?>
